import React from 'react';
import CommonList from '../../../components/CommonLists/list';
import { Button, message } from 'antd';
import services from '../../../services/api';
import Modal from '../../../components/Modal/index';

class List extends CommonList {
  constructor(props) {
    super(props);
    this.api = 'nameListConfig';
    this.operatorWidth = '180px';
    this.editButton = record => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.edit.bind(this, record, { title: '编辑同步配置' })}
          disabled={record.sync_status === 2}
        >
          编辑
        </Button>
      );
    };
    this.delButton = record => {
      return (
        <Button
          size='small'
          type='link'
          disabled={record.sync_status === 2}
          onClick={this.delete.bind(this, record)}
        >
          删除
        </Button>
      );
    };
    this.resetButton = record => {
      return (
        <Button
          size='small'
          type='link'
          onClick={this.reset.bind(this, record)}
        >
          重置同步状态
        </Button>
      );
    };
    this.btnGroup = [this.editButton, this.delButton, this.resetButton];
    this.enumerationRef = React.createRef()
  }

  delete(record) {
    Modal.open({
      title: '',
      isConfirm: true,
      width: '300px',
      content: '确认删除当前名单同步配置信息吗？',
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        services[this.api].delete({ namelist_config_id: record.namelist_config_id  }).then(data => {
          if (data.code === 0) {
            message.success(data.msg);
            this.props.queryList();
            Modal.close();
          } else {
            message.error(data.msg);
          }
        });
      },
      cancel: () => {
        Modal.close();
      }
    });
  }

  reset(record) {
    Modal.open({
      title: '',
      isConfirm: true,
      width: '300px',
      content: '重置同步状态会重新覆盖所有数据，确认同步吗？?',
      isOkBtn: true,
      isCancelBtn: true,
      ok: () => {
        services[this.api].reset({ namelist_config_id: record.namelist_config_id }).then(data => {
          if (data.code === 0) {
            message.success(data.msg);
            this.props.queryList();
            Modal.close();
          } else {
            message.error(data.msg);
          }
        });
      },
      cancel: () => {
        Modal.close();
      }
    });
  }
}

export default List;
